//-------------------------------------------------
//		Quick Pager jquery plugin
//		Created by dan@geckonm.com
//		www.geckonewmedia.com
// 
//-------------------------------------------------

(function($) {
	    
    $.fn.quickPager = function(options) {
	
        var defaults = {
            pageSize: 10,
            currentPage: 1,
            holder: "",
            totalPage:10
        };
        var options = $.extend(defaults, options);
	  	
        //leave this
        var temp=options.totalPage;
        var selector = $(this);
        var totalRecords = $(this).children().length;
        var pageCounter = 1;

        selector.children().each(function(i){
            if(i < pageCounter*options.pageSize ) {
                $(this).addClass("page"+pageCounter);
            }
            else {
                $(this).addClass("page"+(pageCounter+1));
                pageCounter ++;
            }	
        });
		 
        //show/hide the appropriate regions 
        selector.children().hide();
        $(".page"+options.currentPage).show();
		
        //first check if there is more than one page. If so, build nav
        if(pageCounter > 1) {
			
            //Build pager navigation
            var pageNav = "<ul class='pageNav'>";	
            pageNav += "<li class='first' style='cursor: pointer' rel="+(1)+">First</li>";
            for (i=1;i<=pageCounter;i++){
							
                if (i==options.currentPage) {
                    pageNav += "<li class=currentPage pageNav"+i+"'><a class="+i+" rel='"+i+"' href='#'>"+i+"</a></li>";	
                }
                else {
               
                    pageNav += "<li class='pageNav"+i+"'><a class="+i+" rel='"+i+"' href='#'>"+i+"</a></li>";
                    if(i==options.totalPage){
                        pageNav += "<li class='next' style='cursor: pointer' rel="+(i+1)+">Next</li>";
                        break;
                    }
                }
		 	
            }
           if(i==options.totalPage)
            pageNav += "<li class='last' style='cursor: pointer' rel="+(pageCounter)+">Last</li></ul>";	
			
            if(options.holder == "") {
                selector.after(pageNav);
            }
            else {
                $(options.holder).append(pageNav);
            }
            $(".next").live("click",function() {			
                //grab the REL attribute 
                var pageNav1="";
                var i;
                var $old=  options.totalPage+1;
                options.totalPage= options.totalPage+temp;
                pageNav1 += "<li class='first' style='cursor: pointer' rel="+(1)+">First</li>";
                pageNav1 += "<li class='previous' style='cursor: pointer' rel="+($old-1)+">Previous</li>";
                for(i=$old;i<=options.totalPage;i++)
                {
                    pageNav1 += "<li class='pageNav"+i+"'><a class="+i+" rel='"+i+"' href='#'>"+i+"</a></li>";
                    if(i==options.totalPage){
                        pageNav1 += "<li class='next' style='cursor: pointer' rel="+(i+1)+">Next</li>";
                        
                    }
                    if(i==pageCounter)
                        break;
                }
                   pageNav1 += "<li class='last' style='cursor: pointer' rel="+(pageCounter)+">Last</li></ul>";	
                   
              
                $('ul.pageNav"').html(pageNav1);
                var clickedLink = $(this).attr("rel");
             
                options.currentPage = clickedLink;
                //remove current current (!) page
                $("li.currentPage").removeClass("currentPage");
                //Add current page highlighting
                $("ul.pageNav").find("a[rel='"+clickedLink+"']").parent("li").addClass("currentPage");
                //$(this).parent("li").addClass("currentPage");
                //hide and show relevant links				
                selector.children().hide();			
                selector.find(".page"+clickedLink).show();
               
              
                return false;
            });		
            
            $(".previous").live("click",function() {			
                //grab the REL attribute 
                var clickedLink = $(this).attr("rel");
                var pageNav1="";
                var j;
              
      
                options.totalPage= options.totalPage-temp;
                 pageNav1 += "<li class='first' style='cursor: pointer' rel="+(1)+">First</li>";
                if(options.totalPage>temp)
                {  
                       
                    pageNav1 += "<li class='previous' style='cursor: pointer' rel="+(clickedLink-temp)+">Previous</li></ul>";
                }
                   
                    
                for(j=temp-1;j>=0;j--)
                {
                   
                    pageNav1 += "<li class='pageNav"+(clickedLink-j)+"'><a class="+(clickedLink-j)+" rel='"+(clickedLink-j)+"' href='#'>"+(clickedLink-j)+"</a></li>";
                    if(j==0){
                     
                        var ho=parseInt(clickedLink)+parseInt(1);
                        pageNav1 += "<li class='next' style='cursor: pointer' rel='"+ho+"'>Next</li>";
                        
                    }
                       
                }
                    
          pageNav1 += "<li class='last' style='cursor: pointer' rel="+(pageCounter)+">Last</li></ul>";	
                $('ul.pageNav"').html(pageNav1);
               
             
                options.currentPage = clickedLink;
                //remove current current (!) page
                $("li.currentPage").removeClass("currentPage");
                //Add current page highlighting
                $("ul.pageNav").find("a[rel='"+clickedLink+"']").parent("li").addClass("currentPage");
                //$(this).parent("li").addClass("currentPage");
                //hide and show relevant links				
                selector.children().hide();			
                selector.find(".page"+clickedLink).show();
               
              
                return false;
            });			
        
            $(".first").live("click",function() {			
                //grab the REL attribute 
                var pageNav1="";
                var i;
               
                options.totalPage= temp;
                pageNav1 += "<li class='first' style='cursor: pointer' rel="+(1)+">First</li>";
                for(i=1;i<=options.totalPage;i++)
                {
                    pageNav1 += "<li class='pageNav"+i+"'><a class="+i+" rel='"+i+"' href='#'>"+i+"</a></li>";
                    if(i==options.totalPage){
                      
                        pageNav1 += "<li class='next' style='cursor: pointer' rel="+(i+1)+">Next</li>";
                        pageNav1 += "<li class='last' style='cursor: pointer' rel="+(pageCounter)+">Last</li></ul>";
                        
                    }
                    if(i==pageCounter)
                        break;
                }
             
             
                $('ul.pageNav"').html(pageNav1);
                var clickedLink = $(this).attr("rel");
             
                options.currentPage = clickedLink;
                //remove current current (!) page
                $("li.currentPage").removeClass("currentPage");
                //Add current page highlighting
                $("ul.pageNav").find("a[rel='"+clickedLink+"']").parent("li").addClass("currentPage");
                //$(this).parent("li").addClass("currentPage");
                //hide and show relevant links				
                selector.children().hide();			
                selector.find(".page"+clickedLink).show();
               
              
                return false;
            });		
            
            $(".last").live("click",function() {			
                //grab the REL attribute 
                var pageNav1="";
                var i,count=0;
                var clickedLink = $(this).attr("rel");
                if(pageCounter%temp!=0){
                   
                    options.totalPage=(parseInt(pageCounter/temp)+1)*temp;
                    count=pageCounter-(parseInt(pageCounter/temp)*temp);
                 
                }
                else
                {
                    options.totalPage= (pageCounter/temp)*temp;
                    count=temp;
                }
              
                pageNav1 += "<li class='first' style='cursor: pointer' rel="+(1)+">First</li>";
                pageNav1 += "<li class='previous' style='cursor: pointer' rel="+(pageCounter-count)+">Previous</li>";
                for(i=count-1;i>=0;i--)
                {
                    pageNav1 += "<li class='pageNav"+(clickedLink-i)+"'><a class="+(clickedLink-i)+" rel='"+(clickedLink-i)+"' href='#'>"+(clickedLink-i)+"</a></li>";
                 
                }
                   
                    
                $('ul.pageNav"').html(pageNav1);
                
             
                options.currentPage = clickedLink;
                //remove current current (!) page
                $("li.currentPage").removeClass("currentPage");
                //Add current page highlighting
                $("ul.pageNav").find("a[rel='"+clickedLink+"']").parent("li").addClass("currentPage");
                //$(this).parent("li").addClass("currentPage");
                //hide and show relevant links				
                selector.children().hide();			
                selector.find(".page"+clickedLink).show();
               
             
                return false;
            });		
            //pager navigation behaviour
            $(".pageNav a").live("click", function() {			
                //grab the REL attribute 
                var clickedLink = $(this).attr("rel");
             
                options.currentPage = clickedLink;
                //remove current current (!) page
                $("li.currentPage").removeClass("currentPage");
                //Add current page highlighting
                $("ul.pageNav").find("a[rel='"+clickedLink+"']").parent("li").addClass("currentPage");
                //$(this).parent("li").addClass("currentPage");
                //hide and show relevant links				
                selector.children().hide();			
                selector.find(".page"+clickedLink).show();
                  
                return false;
            });
	
        }
			  
    }
	

})(jQuery);
